﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BitAuto.EP.Plat.Util;

namespace CRMSimple.LogServices
{
    public partial class LogService : ServiceBase
    {
        public LogService()
        {
            InitializeComponent();
        }
        private Thread threadproducter = null;
        private Thread threadconsumer = null;
        private Thread threaddeleteProcess = null;
        LogServiceContextRoot producter = null;
        LogServiceContextRoot consumer = null;
        DeleteFileProcess deleteProcess = new DeleteFileProcess();

        protected override void OnStart(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            LogExceptionRoot.ContextLog.Info("服务启动");
            try
            {
                producter = new LogServiceContextRoot()
                             .UseRMQProducter()
                             .UseTextFileDataCache()
                             .Buid();
                consumer = new LogServiceContextRoot()
                                     .UseRMQConsumer()
                                     .UseTextFileDataCache()
                                     .Buid();
            }
            catch (Exception ex)
            {
                //DataLogHelper.E("OnStart", ex);
                LogExceptionRoot.ContextLog.Info("OnStart", ex);

                throw;
            }
        

            // 生产数据            
            if (threadproducter == null)
            {
                threadproducter = new Thread(new ParameterizedThreadStart(producter.Run));
            }
            threadproducter.IsBackground = true;
            threadproducter.Start(producter);
            //处理数据            
            if (threadconsumer == null)
            {
                threadconsumer = new Thread(new ParameterizedThreadStart(consumer.Run));
            }
            threadconsumer.IsBackground = true;
            threadconsumer.Start(consumer);
            ////删除文件
            if (threaddeleteProcess == null)
            {
                threaddeleteProcess = new Thread(new ParameterizedThreadStart(deleteProcess.Run));
            }
            threaddeleteProcess.IsBackground = true;
            threaddeleteProcess.Start(deleteProcess);
            
        }

        protected override void OnStop()
        {
            LogExceptionRoot.ContextLog.Info("服务停止");
            producter.Stop();
            consumer.Stop();
            deleteProcess.Stop();
            if (threadproducter != null)
            {
                threadproducter.Abort();
            }
            if (threadconsumer != null)
            {
                threadconsumer.Abort();
            }
            if (threaddeleteProcess != null)
            {
                threaddeleteProcess.Abort();
            }
            
        }
    }
}
